﻿namespace Amazon.Checkout.Controllers
{
    using System;
    using System.Web.Mvc;
    using Orchard;
    using Orchard.Localization;
    using Orchard.Logging;
    using Orchard.Themes;
    using Orchard.UI.Admin;
    using Orchard.UI.Notify;
    using Orchard.Utility.Extensions;
    using Services;
    using ViewModels;

    [Themed]
    [Admin]
    public class ShippingController : Controller
    {
        private readonly ICheckoutByAmazonService amazonService;
        private readonly IOrchardServices orchardServices;
        private readonly Localizer T;

        public ShippingController(ICheckoutByAmazonService amazonService, IOrchardServices services)
        {
            this.amazonService = amazonService;
            this.orchardServices = services;

            this.T = NullLocalizer.Instance;
        }

        [HttpGet]
        public ActionResult List()
        {
            var model = new ShippingListViewModel { Shippings = this.amazonService.GetShippings() };
            return View(model);
        }

        [HttpGet]
        public ActionResult Delete(int shippingId)
        {
            var shipping = this.amazonService.GetShipping(shippingId);
            if (shipping == null)
            {
                this.orchardServices.Notifier.Error(this.T("Deleting Shipping failed: shipping {0} does not exist.", shippingId));
                return RedirectToAction("List");
            }

            this.amazonService.DeleteShipping(shippingId);

            this.orchardServices.Notifier.Information(this.T("Shipping {0} deleted.", shipping.Id));

            return RedirectToAction("List");
        }

        [HttpGet]
        public ActionResult Edit(int shippingId)
        {
            var shipping = this.amazonService.GetShipping(shippingId);

            var viewModel = new ShippingNewUpdateViewModel
            {
                ShippingId = shippingId,
                DisplayableShippingLabel = shipping.DisplayableShippingLabel,
                ExcludedRegionsString = shipping.ExcludedRegionsString,
                ExcludedRegions = shipping.GetExcludedRegions(),
                IncludedRegionsString = shipping.IncludedRegionsString,
                IncludedRegions = shipping.GetIncludedRegions(),
                ServiceLevel = shipping.ServiceLevel,
                PoBoxSupported = shipping.PoBoxSupported,
                ShipmentRateType = shipping.ShipmentRateType,
                ItemQuantityBasedShippingRate = shipping.ItemQuantityBasedShippingRate,
                ShipmentBasedShippingRate = shipping.ShipmentBasedShippingRate,
                WeightBasedShippingRate = shipping.WeightBasedShippingRate
            };

            return View("NewEdit", viewModel);
        }

        [HttpPost]
        public ActionResult Edit(ShippingNewUpdateViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return View("NewEdit", viewModel);
            }

            var shipping = this.amazonService.GetShipping(viewModel.ShippingId);

            if (shipping == null)
            {
                this.orchardServices.Notifier.Error(this.T("Updating Shipping {0} failed: shipping does not exists", viewModel.ShippingId));
                return View("NewEdit", viewModel);
            }

            this.amazonService.UpdateShipping(
                    viewModel.ShippingId,
                    viewModel.IncludedRegionsString,
                    viewModel.ExcludedRegionsString,
                    viewModel.ShipmentRateType,
                    viewModel.ShipmentBasedShippingRate,
                    viewModel.WeightBasedShippingRate,
                    viewModel.ItemQuantityBasedShippingRate,
                    viewModel.PoBoxSupported,
                    viewModel.DisplayableShippingLabel,
                    viewModel.ServiceLevel);

            return RedirectToAction("List");
        }

        [HttpGet]
        public ActionResult New()
        {
            return View("NewEdit", new ShippingNewUpdateViewModel());
        }

        [HttpPost]
        public ActionResult New(ShippingNewUpdateViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return View("NewEdit", viewModel);
            }

            try
            {
                this.amazonService.CreateShipping(
                    viewModel.IncludedRegionsString,
                    viewModel.ExcludedRegionsString,
                    viewModel.ShipmentRateType,
                    viewModel.ShipmentBasedShippingRate,
                    viewModel.WeightBasedShippingRate,
                    viewModel.ItemQuantityBasedShippingRate,
                    viewModel.PoBoxSupported,
                    viewModel.DisplayableShippingLabel,
                    viewModel.ServiceLevel);

                this.orchardServices.Notifier.Information(this.T("Shipping created"));
                return RedirectToAction("List");
            }
            catch (Exception exception)
            {
                this.Error(exception, this.T("Creating Shipping failed: {0}", exception.Message), NullLogger.Instance, this.orchardServices.Notifier);

                return View("NewEdit", viewModel);
            }
        }
    }
}